package com.dems.admin.security.impl;

import com.alibaba.fastjson.JSONObject;
import com.dems.admin.security.IAdminUserService;
import com.dems.core.exception.LoanassistRuntimeException;
import com.dems.dao.dataobject.Menu;
import com.dems.dao.dataobject.StaffRole;
import com.dems.dao.dataobject.User;
import com.dems.dao.mapper.la.MenuMapper;
import com.dems.dao.mapper.la.StaffRoleMapper;
import com.dems.dao.mapper.la.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminUserServiceImpl implements IAdminUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StaffRoleMapper staffRoleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public User findByLoginName(String loginName) {
        User user = userMapper.selectByPhoneNo(loginName);

        if (user != null) {
            user.setPersonIntroduction(null);
        }

        if (user != null && StringUtils.isNotBlank(user.getProfile())) {
            JSONObject userProfileJson = JSONObject.parseObject(user.getProfile());
            userProfileJson.remove("sipToken");
            user.setProfile(userProfileJson.toJSONString());
        }

        if (user != null) {
            List<StaffRole> staffRoleList = staffRoleMapper.selectByUserId(user.getUserId());
            if (CollectionUtils.isNotEmpty(staffRoleList)) {
                user.setRoleCodeList(staffRoleList.stream().map(StaffRole::getRoleCode).collect(Collectors.toList()));
            }
            if (CollectionUtils.isNotEmpty(staffRoleList)) {
                List<String> roleCodeList = staffRoleList.stream().map(StaffRole::getRoleCode).collect(Collectors.toList());
                List<String> userTypeList = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(roleCodeList)) {
                    userTypeList.addAll(roleCodeList);
                }
                List<Menu> menuList = menuMapper.selectByRoleCode(userTypeList);
                if (CollectionUtils.isNotEmpty(menuList)) {
                    user.setMenuCodeList(menuList.stream().map(Menu::getMenuCode).collect(Collectors.toList()));
                }
            }
        }
        return user;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectByPhoneNo(username);
        if(user == null){
            throw new LoanassistRuntimeException("用户不存在: " + username);
        }

        if (user != null) {
            user.setPersonIntroduction(null);
        }

        if (StringUtils.isNotBlank(user.getProfile())) {
            JSONObject userProfileJson = JSONObject.parseObject(user.getProfile());
            userProfileJson.remove("sipToken");
            user.setProfile(userProfileJson.toJSONString());
        }

        List<StaffRole> staffRoleList = staffRoleMapper.selectByUserId(user.getUserId());
        if (CollectionUtils.isNotEmpty(staffRoleList)) {
            user.setRoleCodeList(staffRoleList.stream().map(StaffRole::getRoleCode).collect(Collectors.toList()));
        }

        if (CollectionUtils.isNotEmpty(staffRoleList)) {
            List<String> roleCodeList = staffRoleList.stream().map(StaffRole::getRoleCode).collect(Collectors.toList());
            List<String> userTypeList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(roleCodeList)) {
                userTypeList.addAll(roleCodeList);
            }
//            List<Menu> menuList = menuMapper.selectByRoleCode(userTypeList);
//            if (CollectionUtils.isNotEmpty(menuList)) {
//                user.setMenuCodeList(menuList.stream().map(Menu::getMenuCode).collect(Collectors.toList()));
//            }
        }

        return new org.springframework.security.core.userdetails.User(JSONObject.toJSONString(user), user.getPassword(), Collections.emptyList());
    }
}
